//    Copyright (C) Kherty.  All rights reserved.
using System.Collections.Generic;
using OpenLS.Core.Serialization;

namespace OpenLS.Chart
{
    ///<summary>
    /// Represents a 2D area chart
    ///</summary>
    public class AreaChart : AreaChartBase
    {
        public AreaChart()
        {
            FirstAxis = new CategoryAxis {Position = AxisPosition.Bottom};
            SecondAxis = new ValueAxis {Position = AxisPosition.Left, CrossAxis = FirstAxis};
            FirstAxis.CrossAxis = SecondAxis;
           }

        public AreaChart(Axis firstAxis, Axis secondAxis)
        {
            FirstAxis = firstAxis;
            SecondAxis = secondAxis;
            FirstAxis.CrossAxis = SecondAxis;
            SecondAxis.CrossAxis = FirstAxis;
        }
        ///<summary>
        /// Get the vertical axis
        ///</summary>
        public Axis VerticalAxis
        {
            get
            {
                switch (FirstAxis.Position)
                {
                    case AxisPosition.Left:
                    case AxisPosition.Right:
                        return FirstAxis;
                    default:
                        return SecondAxis;
                }
            }
        }
        ///<summary>
        /// Get the horizontal axis
        ///</summary>
        public Axis HorizontalAxis
        {
         get
         {
             switch(FirstAxis.Position)
             {
                 case AxisPosition.Bottom:
                    case AxisPosition.Top:
                     return FirstAxis;
                     default:
                     return SecondAxis;
             }
         }
        }

        public DataSource Categories
        {
            get
            {
                if (Series.Count == 0)
                    return null;
                AreaSeries firstSeries = Series[0];
                DataSource ds = firstSeries.Categories;
                if (ds == null)
                {
                    int count = GetDisplayInformation().Count;
                    return StringLiteral.NewStringLiteral(1, count);
                }
                return ds;
            }
        }


        private Axis FirstAxis { get; set; }

        private Axis SecondAxis { get; set; }


        protected override void VerifyAxes()
        {
            VerticalAxis.CrossAxis = HorizontalAxis;
            HorizontalAxis.CrossAxis = VerticalAxis;
        }

        internal override void ReadXmlCore(ReadContext context)
        {
            using (var c = context.Read("areaChart"))
            {
                ReadXmlBase(c);
                AxisResolver r = ReadContextData.GetAxisResolver(c);
                r.AddAction(c.ReadElementUIntegerAttribute("axId"), delegate(Axis a) { FirstAxis = a; });
                r.AddAction(c.ReadElementUIntegerAttribute("axId"), delegate(Axis a) { SecondAxis = a; });
                Extension = c.ReadOptionalElement<ExtensionElement>("extLst");
            }
        }

        internal override void WriteXmlCore(WriteContext context)
        {
            using (WriteContext c = context.Write("areaChart"))
            {
                WriteXmlBase(c);
                c.WriteElementUIntegerAttribute("axId", FirstAxis.Id);
                c.WriteElementUIntegerAttribute("axId", SecondAxis.Id);
                c.WriteOptionalElement(Extension);
            }
        }

        internal override IEnumerable<Axis> GetAxes()
        {
            yield return FirstAxis;
            yield return SecondAxis;
        }
    }
}